﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Deloitte.Controls
{
    public partial class BindingListFilterDataGridViewControl<T> : UserControl        
    {        
        BindingListFilterControl<T> _filterControl = new BindingListFilterControl<T>(null);

        public BindingListFilterDataGridViewControl(BindingList<T> list) : this(list, string.Empty, null)
        {
            
        }
        public BindingListFilterDataGridViewControl(BindingList<T> list, string selectColumnPropertyName, List<string> columnNameOrder)
        {
            InitializeComponent();
            Deloitte.DataGridViewHelper.FormatDataGridViews(this);
            Deloitte.DataGridViewHelper.AddErrorEventHandler(this);
            this._filterControl.List = list;
            this.panel1.Controls.Add(_filterControl);
            this.panel1.Controls[_filterControl.Name].Dock = DockStyle.Fill;
            this._filterControl.ListFilteredEvent += new BindingListFilterControl<T>.ListFilteredEventHandler(_filterControl_ListFilteredEvent);
            
            this.List = list;

            if (!string.IsNullOrEmpty(selectColumnPropertyName))
            {
                this.buttonsPanel.Visible = true;
                this.SelectColumnPropertyName = selectColumnPropertyName;
            }
            else
            {
                this.buttonsPanel.Visible = false;
            }

            if (columnNameOrder != null)
            {
                int i = 0;
                foreach (string columnName in columnNameOrder)
                {
                    foreach(dataGridView
                    i++;
                }
            }
        }

        private string _selectColumnPropertyName;

        private string SelectColumnPropertyName
        {
            get { return _selectColumnPropertyName; }
            set { _selectColumnPropertyName = value; }
        }

        public DataGridView DataGridView
        {
            get { return this.dataGridView; }
        }

        void _filterControl_ListFilteredEvent(object sender, BindingListFilterControl<T>.ListFilteredEventArgs e)
        {
            this.List = e.FilteredList;
        }

        private void SetFilterControlList(BindingList<T> list)
        {
            this._filterControl.List = list;            
        }

        public System.Windows.Forms.BindingSource BindingSource
        {
            get { return _bindingSource; }
            set { _bindingSource = value; }
        }

        private void SetDataSources(BindingList<T> list)
        {
            //Type dataSourceType=null;
            //if (this.BindingSource.DataSource != null)
            //{
            //    dataSourceType = this.BindingSource.DataSource.GetType();
            //}
            if (this.BindingSource == null) CreateBindingSource();
            this.BindingSource.DataSource = list;
            this.dataGridView.DataSource = this.BindingSource;
            SetDataGridViewFormat(list);            
        }

        private void CreateBindingSource()
        {
            this.BindingSource = new BindingSource();
        }

        private void SetDataGridViewFormat(BindingList<T> list)
        {           
            this.dataGridView.Columns.Clear();
            if (!typeof(T).Name.Equals("String"))
            {
                if (list.Count > 0)
                {
                    Reflection.BindPropertiesToDataGridView(list[0].GetType(), dataGridView);                    
                }
            }
            else { throw new NotImplementedException("Matt needs to implement this"); }
        }

        private BindingList<T> _list;

        public BindingList<T> List
        {
            get { return _list; }
            set
            {
                //if (value.GetType().FullName != _list.GetType().FullName) { 
                
            //}

                _list = value;                
                 
                if (_list != null)
                {                    
                    SetDataSources(_list);
                    if (this._filterControl.List == null) this.SetFilterControlList(_list);
                }
            }
        }

        private void selectAllButton_Click(object sender, EventArgs e)
        {
            SetSelectableColumnValue(true);
        }

        private void SetSelectableColumnValue(bool val)
        {
            foreach (DataGridViewRow row in dataGridView.Rows)
            {
                try { row.Cells[this.SelectColumnPropertyName + "DataGridViewCheckBoxColumn"].Value = val; }
                catch { }
            }
        }

        private void deselectAllButton_Click(object sender, EventArgs e)
        {
            SetSelectableColumnValue(false);
        }        
    }
}